package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void setOrderSettingByDay(OrderSetting orderSetting) {
        OrderSetting orderSetting1 = orderSettingDao.findOrderSettingByDate(orderSetting.getOrderDate());
        if (orderSetting1 != null) {
            orderSettingDao.update(orderSetting);
        } else {
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public List<Map> findOderSettingByMonth(String date) {
        List<OrderSetting> orderSettings = orderSettingDao.findOderSettingByMonth(date);
        List<Map> list = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map<String, Integer> map = new HashMap();
            map.put("date", orderSetting.getOrderDate().getDate());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return list;
    }

    @Override
    public void upload(List<String[]> strings) {
        try {
            for (String[] string : strings) {
                if (string.length != 2) {
                    return;
                }
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setNumber(Integer.parseInt(string[1]));
                orderSetting.setOrderDate(new Date(string[0]));
                setOrderSettingByDay(orderSetting);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
